/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import bean.RawMessage;
import bean.SMSCNodeConfiguration;
import java.util.Timer;
import java.util.TimerTask;
import main.SMSCConnection.Status;

/**
 *
 * @author enemY
 */


public class Main {

    
    public static void main(String[] args) {
        int ii=0;
     while (true){ System.out.println("kuku"+ii);
           ii++;
        String[][] ss=functions.getResult("select * from smsc.schedule where last_status_id=0");
        String sql="";
            for (int i=0;i<ss.length; i++){
                   String res=SendSMS(ss[i][2], ss[i][3]);
                   if (res.equals("ok")){
                       sql+="update smsc.schedule set last_status_id=1 where id="+ss[i][0]+";\n";
                   }
            }
            functions.execSql(sql);
            try{
                Thread.sleep(1000);
            }catch(Exception  e){
                
            }
  if (ii>20) break;
        System.out.println("1111111111111111");
     }
    }
    public static String SendSMS (String Number,String Body)
    {
        System.out.println("number="+Number+"  body="+Body);
        SMSCNodeConfiguration cfg = new SMSCNodeConfiguration();
        cfg.setServerAddress("91.151.128.64");
        cfg.setServerPort(9022);
        cfg.setSourceAddress("SOCAR");
        // user/pass 
        cfg.setSystemId("SocarSCR");
        cfg.setPassword("SC56r99");
        // timers
        cfg.setEnqLinkFreq(30);
        cfg.setReconnectInterval(5);
        // interface version 3.4
        cfg.setIfaceVersion(0x34);
        
        SMSCConnection conn = new SMSCConnection(cfg);
        conn.start();
        try {
            Thread.sleep(cfg.getReconnectInterval() * 1000 * 2);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        if (conn.getStatus() == Status.CONNECTED) {
            RawMessage msg = new RawMessage();
        //    msg.setRecipientMSISDN("995577105798");
    
            msg.setRecipientMSISDN(Number);
            msg.setMessageType(RawMessage.MessageType.PLAIN);
            msg.setRawMessage(Body);
//            msg.setRawMessage("Hello I Am SocarGeorgiaGas SMS Center. Warning "
//                    + "CRM Dialogue Service is On "
//                    + " please Prepare necessary Procerures ");
            conn.submitSM(msg, conn.getNextSequenceNumber(), true);
        }
        try {
            conn.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return "ok";
    }
}
